<?php
/**
 * Created by PhpStorm.
 * User: zouyanan
 * Date: 2019/5/11
 * Time: 下午10:35
 */

namespace app\api\controller;
use app\common\controller\Api;
use fast\Pinyin;
use think\Db;
use think\paginator\driver\Bootstrap;

class House extends Api
{
    protected $noNeedLogin = ['houseAttribute','houseList','layout','herHouse','houseNear','video','houseDetailEdit','houseDetailEditOperate'];
    protected $noNeedRight = '*';
    protected $websiteUrl = 'http://api.yjzufang.com';

    //房东信息添加
    public function landlordAdd(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $userInfo = $this->auth->getUserinfo();
        $data['agent_id'] = $userInfo['id'];

        $data['name'] = $this->request->request('name');
        $data['pinyin'] = Pinyin::get($data['name'],false);
        $data['first'] = substr(Pinyin::get($data['name'],true), 0 , 1);

        $data['idcard'] = $this->request->request('idcard');
        $data['mobile'] = $this->request->request('mobile');
        $data['wx'] = $this->request->request('wx');
        $data['qq'] = $this->request->request('qq');
        $data['remark'] = $this->request->request('remark');
        $data['is_sync'] = $this->request->request("is_sync");
        $data['createtime'] = time();
        if(!$data['name'] || !$data['mobile']){
            $this->error("缺少参数");
        }
        $res = db("yjy_house_landlord")->insert($data);
        if($res){
            $this->success("添加成功");
        }else{
            $this->error("添加失败");
        }
    }


    //租客信息添加
    public function renterAdd()
    {
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $userInfo = $this->auth->getUserinfo();
        $house_id = $this->request->request("house_id");
        $data['agent_id'] = $userInfo['id'];
        $data['name'] = $this->request->request('name');
        $data['idcard'] = $this->request->request('idcard');
        $data['mobile'] = $this->request->request('mobile');
        $data['wx'] = $this->request->request('wx');
        $data['qq'] = $this->request->request('qq');
        $data['remark'] = $this->request->request('remark');
        $data['is_sync'] = $this->request->request("is_sync");
        $data['createtime'] = time();
        $data['image'] = $this->request->request("image"); //合同
        if (!$data['name'] || !$data['mobile']) {
            $this->error('缺少参数');
        }
        $renter_id = db("yjy_house_renter")->insertGetId($data);
        $res = db("yjy_house")->where("house_id",$house_id)->update(['renter_id'=>$renter_id]);
        if ($res) {
            $this->success('信息添加成功');
        } else {
            $this->error('信息添加失败');
        }

    }


    //上传
    function Upload()
    {
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        ob_clean();
        $userInfo = $this->auth->getUserinfo();
        $type = $this->request->param("type");//图片类型 合同=agreement
        $file = $this->request->file("file");
        $house_id = $this->request->request("house_id");

        $img = "";
        if ($file) {
            $info = $file->move('./uploads/'.$type.'/');
            if ($info) {
                $getSaveName = str_replace("\\", "/", $info->getSaveName());
                $img_name = $getSaveName;
                $img = "/uploads/".$type."/".$img_name;
            } else {
                $this->error('上传错误'.$file->getError());
            }
        }
        $infos['avatar'] = $img;
        if($type == 'pay' || $type == 'cert'){
            $this->success("上传成功","/uploads/".$type."/".$img_name);
        }elseif($type == 'avatar'){
            $res = db('user')->where('id',$userInfo['id'])->update($infos);
            if($res){
                $this->success('上传成功',"/uploads/".$type."/".$img_name);

            }else{
                $this->error('上传失败');
            }
        }
    }



   //发布房源
    public function housePublish(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $userInfo = $this->auth->getUserinfo();
       // $files = $this->request->file("file");
        $title = $this->request->request("title");
        $houseArea = $this->request->request("house_area");
        $houseOrientations = $this->request->request("house_orientations");
        $houseFloor = $this->request->request("house_floor");
        $content = $this->request->request("content");
        $houseConfig = $this->request->request("house_config");
        $houseLayout = $this->request->request("house_layout");
        $area_id = $this->request->request("area_id");
        $houseTags = $this->request->request("house_tags");
        $price = $this->request->request("price");
        $location_near = $this->request->request("location_near");

        $location_address = $this->request->request("location_address");
        $lon = $this->request->request("lon");
        $lat = $this->request->request("lat");


        $is_recommend = $this->request->request("is_recommend");
        $landlord_id = $this->request->request("landlord_id");
        $images = $this->request->request("images");
        $video = $this->request->request("video");
        if($is_recommend == true){$is_recommend = 1;}
        if($is_recommend == false){$is_recommend = 0;}

        $arr_config = explode(",", $houseConfig);
        $config_ids = [];
        foreach($arr_config as $val){
            $config = db("yjy_house_config")->field("id,name")->select();
            foreach($config as $k=>$v){
                if($val == $v['name']){array_push($config_ids,$v['id']);}
            }
        }
        $config_ids_2 = implode(',',$config_ids);


        $arr_tags = explode(",", $houseTags);
        $tags_ids = [];
        foreach($arr_tags as $val){
            $tags = db("yjy_house_tags")->field("id,name")->select();
            foreach($tags as $k=>$v){
                if($val == $v['name']){array_push($tags_ids,$v['id']);}
            }
        }
        $tags_ids_2 = implode(',',$tags_ids);
        $params_house=[
            'title'       =>$title,
            'house_area'  =>$houseArea,
            'orientations_id' =>$houseOrientations,
            'house_floor' =>$houseFloor,
            'content'       =>$content,
            'config_ids'=>$config_ids_2,
            'layout_id'=>$houseLayout,
            'area_id'  =>$area_id,
            'tags_ids'  =>$tags_ids_2,
            'price'       =>$price,
            'location_near'=>$location_near,
            'location_map'=>$location_address,
            'lon'        =>$lon,
            'lat'        =>$lat,
            'is_recommend'=>$is_recommend,
            'createtime'  =>time(),
            'user_id'     =>$userInfo['id'],
            'landlord_id' =>$landlord_id,
            'images'      =>$images,
            'video'       =>$video
        ];
        $res = db("yjy_house")->insertGetId($params_house);
        if($res){
            $this->success("房源添加成功",$res);
        }else{
            $this->error("房源添加失败");
        }
    }



    /*
     * 房源属性
     */
     public function houseAttribute(){
         header('Access-Control-Allow-Origin: *');
         header('Access-Control-Allow-Credentials: true');
         header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
         header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
         $list_config = db("yjy_house_config")->field("name,name as value")->select();
       //  $arr_config = [];
        // foreach($list_config as $key=>$val){
            // $arr_config[$val['id']] = $val['name'];
        // }

         $list_layout = db("yjy_house_layout")->field("*")->select();
         $arr_layout = [];
         foreach($list_layout as $key=>$val){
             $arr_layout[$val['id']] = $val['name'];
         }

         $list_tags = db("yjy_house_tags")->field("name,name as value")->select();
         //$arr_tags = [];
         //foreach($list_tags as $key=>$val){
            // $arr_tags[$val['id']] = $val['name'];
        // }

         $list_area = db("yjy_house_area")->field("id,name")->select();
         $arr_area = [];
         foreach($list_area as $key=>$val){
             $arr_area[$val['id']] = $val['name'];
         }

         $list_orientations = db("yjy_house_orientations")->field("id,name")->select();
         $arr_orientations = [];
         foreach($list_orientations as $key=>$val){
             $arr_orientations[$val['id']] = $val['name'];
         }

         $data=[
           'list_config'=>$list_config,
           'list_layout'=>$arr_layout,
           'list_tags'  =>$list_tags,
            'list_area' =>$arr_area,
           'list_orientations' => $arr_orientations
         ];
         $this->success("返回成功",$data);
    }

    //户型/区域
    public function layout(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $list_layout = db("yjy_house_layout")->field("*")->select();
        $layout = [];
        foreach($list_layout as $key=>$val){
            $layout[$key]['title'] = $val['name'];
            $layout[$key]['value'] = $val['id'];
        }

        $list_area = db("yjy_house_area")->field("*")->select();
        $area = [];
        foreach($list_area as $key=>$val){
            $area[$key]['title'] = $val['name'];
            $area[$key]['value'] = $val['id'];
        }

        $list_orientations = db("yjy_house_orientations")->field("*")->select();
        $orientations = [];
        foreach($list_orientations as $key=>$val){
            $orientations[$key]['title'] = $val['name'];
            $orientations[$key]['value'] = $val['id'];
        }
        $data= [
          'layout' => $layout,
          'area'   => $area,
          'orientations'=>$orientations
        ];

        $this->success("返回成功",$data);
    }


    //添加看房记录
    public function houseVisitAdd(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $userInfo = $this->auth->getUserinfo();
        $data['user_id'] = $userInfo['id'];
        $data['house_id'] = $this->request->request("house_id");
        $data['renter_name'] = $this->request->request("renter_name");
        $data['mobile'] = $this->request->request("mobile");
        $data['createtime'] = strtotime($this->request->request("visit_time"));
        if(!$data['house_id'] || !$data['renter_name'] || !$data['mobile']){
            $this->error("缺少参数");
        }
        $res = db("yjy_house_visit_log")->insert($data);
        if($res){
            $this->success("添加记录成功");
        }else{
            $this->error("添加记录失败");
        }
    }


    //全部房源/热门推荐
    public function houseList(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $action = $this->request->request("action");
        $layout = $this->request->request("layout");
        $area_id = $this->request->request("area_id");
        $price = $this->request->request("price");
        $sort = $this->request->request("sort");
        $keywords = $this->request->request("keywords");
        $page = $this->request->request("page");
        $where = [];
        if($layout){
            $where['a.layout_id'] = $layout;
         }
        if($area_id){
            $where['a.area_id'] = $area_id;
        }
        if($price){
            if($price == 1){
                $where['a.price'] = array('between','1000,2000');
            }
            if($price == 2){
                $where['a.price'] = array("between",'2000,3000');
            }
            if($price == 3){
                $where['a.price'] = array("between",'3000,4000');
            }
            if($price == 4){
                $where['a.price'] = array("between",'9000,10000');
            }
            if($price == 5){
                $where['a.price'] = array("gt",'10000');
            }
        }
        if($sort == 'addtime'){
            $order = array("createtime"=>'asc');
        }elseif($sort == 'low'){
            $order = array("price"=>'asc');
        }elseif($sort == 'high'){
            $order = array("price"=>'desc');
        }else{
            $order = array("id"=>'desc');
        }

        if($action == 'hot' && !$keywords) {
            $list = db('yjy_house a')
                ->join("yjy_house_layout b", "a.layout_id = b.id", "LEFT")
                ->field("a.id,a.title,a.image,a.location_near,a.price,b.name as layout,a.status,a.images")
                ->where("a.is_recommend",'1')
                ->where("a.house_status",'1')
                ->paginate(6);

        }elseif($keywords){
            $list = db('yjy_house a')
                ->join("yjy_house_layout b", "a.layout_id = b.id", "LEFT")
                ->field("a.id,a.title,a.image,a.location_near,a.price,b.name as layout,a.status,a.images")
                ->where('title', 'like', '%'.$keywords.'%')
                ->paginate(6);
        }else{
            $list = db('yjy_house a')
                ->join("yjy_house_layout b", "a.layout_id = b.id", "LEFT")
                ->field("a.id,a.title,a.image,a.location_near,a.price,b.name as layout,a.status,a.images")
                ->where($where)
                ->order($order)
                ->where("a.house_status",1)
                ->paginate(6);
        }
        $this->success("返回成功",$list);
    }



    //附近房源
    public function houseNear(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $lon = $this->request->request("lon");
       $lat = $this->request->request("lat");
       // $lon = 117.416664123535;
        //$lat = 32.466667175293;
        $layout = $this->request->request("layout");
        $area_id = $this->request->request("area_id");
        $price = $this->request->request("price");
        $sort = $this->request->request("sort");

        $where = [];
        if($layout){
            $where['a.layout_id'] = $layout;
        }
        if($area_id){
            $where['a.area_id'] = $area_id;
        }
        if($price){
            if($price == 1){
                $where['a.price'] = array('between','1000,2000');
            }
            if($price == 2){
                $where['a.price'] = array("between",'2000,3000');
            }
            if($price == 3){
                $where['a.price'] = array("between",'3000,4000');
            }
            if($price == 4){
                $where['a.price'] = array("between",'9000,10000');
            }
            if($price == 5){
                $where['a.price'] = array("gt",'10000');
            }
        }
        if($sort == 'addtime'){
            $order = array("createtime"=>'asc');
        }elseif($sort == 'low'){
            $order = array("price"=>'asc');
        }elseif($sort == 'high'){
            $order = array("price"=>'desc');
        }else{
            $order = array("createtime"=>'desc');
        }
         $list = db("yjy_house a")
            ->field("ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($lat*PI()/180-lat*PI()/180)/2),2)+COS($lat*PI()/180)*COS(lat*PI()/180)*POW(SIN(($lon*PI()/180-lon*PI()/180)/2),2)))*1000) as distance,a.*")
             ->where($where)
             ->order($order)
             ->where("a.house_status",1)
             ->having('distance <= 1000')
            ->select();
        $this->success("返回成功",$list);
    }




    //我的房源
    public function myHouse(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $userInfo = $this->auth->getUserinfo();
        $layout = $this->request->request("layout");
        $area_id = $this->request->request("area_id");
        $price = $this->request->request("price");
        $sort = $this->request->request("sort");
        $page = $this->request->request("page");
        $where = [];
        if($layout){
            $where['a.layout_id'] = $layout;
        }
        if($area_id){
            $where['a.area_id'] = $area_id;
        }
        if($price){
            if($price == 1){
                $where['a.price'] = array('between','1000,2000');
            }
            if($price == 2){
                $where['a.price'] = array("between",'2000,3000');
            }
            if($price == 3){
                $where['a.price'] = array("between",'3000,4000');
            }
            if($price == 4){
                $where['a.price'] = array("between",'9000,10000');
            }
            if($price == 5){
                $where['a.price'] = array("gt",'10000');
            }
        }
        if($sort == 'addtime'){
            $order = array("createtime"=>'asc');
        }elseif($sort == 'low'){
            $order = array("price"=>'asc');
        }elseif($sort == 'high'){
            $order = array("price"=>'desc');
        }else{
            $order = array("createtime"=>'desc');
        }
        $list = db("yjy_house a")
            ->join("yjy_house_layout b","a.layout_id = b.id","LEFT")
            ->field("a.id,a.title,a.image,a.location_near,a.price,b.name as layout,a.status,a.images")
            ->where("a.user_id",$userInfo['id'])
            ->where($where)
            ->where("a.house_status",1)
            ->order($order)
           // ->order("createtime desc")
            ->paginate(6);
        $this->success("返回成功",$list);
    }

    //他的房源
    public function herHouse(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $user_id = $this->request->request("user_id");
        $layout = $this->request->request("layout");
        $where = [];
        if($layout){
            $where['a.layout_id'] = $layout;
        }
        $list = db("yjy_house a")
            ->join("yjy_house_layout b","a.layout_id = b.id","LEFT")
            ->field("a.id,a.title,a.image,a.location_near,a.price,b.name as layout,a.images")
            ->where("a.user_id",$user_id)
            ->where($where)
            ->select();
       // foreach($list as $key=>$val){
           // $list[$key]['image'] = $this->websiteUrl.$val['image'];
        //}
        $this->success("返回成功",$list);

    }


    // 房源详情
    public function detail(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        ob_clean();

        $userInfo = $this->auth->getUserinfo();
        $house_id = $this->request->request("house_id");
        $house_status = db("yjy_house")->where("Id",$house_id)->value('house_status');
        if($house_status ==2){
            $this->error("该房源已下架",'',1000);
        }
        //基本信息
        $info = db("yjy_house a")
            ->join("user b","a.user_id = b.id","LEFT")
            ->join('yjy_house_layout c',"c.id = a.layout_id","LEFT")
            ->join("yjy_house_orientations d",'d.id = a.orientations_id','LEFT')
            ->field("b.id as agent_id,b.avatar,b.nickname,b.mobile")
            ->field("a.*,c.name as layout,a.video,d.name as house_orientations")
           // ->field("from_unixtime(closingtime,'%Y/%m/%d') as closingtime")
            ->where("a.id",$house_id)
            ->find();
        //房源成交时间
        if($info['status'] == 1){
            $info['agreementTime'] = date( "Y-m-d",db("yjy_deal")->where("house_id",$house_id)->order("createtime desc")->value("createtime"));
        }

        $info['avatar'] = $this->websiteUrl.$info['avatar'];
        //顶部图片信息
        $images_info = explode(",",$info['images']);
        $topImage = [];
        foreach($images_info as $key=>$val){
            $topImage[$key]['image'] = $val;
            $topImage[$key]['type'] = 'img';
        }
        if($info['video']){
            array_push($topImage,['type'=>'video','image'=>'/uploads/20200115/c10f4a694b634de851182d8447cf6395.jpg']);
        }

        //房源标签信息
        $arr = [];
        $tagsInfo = db("yjy_house_tags")->select();
        foreach($tagsInfo as $key=>$val){
            $arr[$val['id']] = $val['name'];
        }
        $arr_tags = [];
        $tags_ids = explode(",",$info['tags_ids']);
        foreach($tags_ids as $v){
            array_push($arr_tags,$arr[$v]);
        }
        //房间配置
        $configInfo = db("yjy_house_config")->select();
        $arr_name = [];
        $arr_image = [];
        foreach($configInfo as $key=>$val){
            $arr_name[$val['id']] = $val['name'];
            $arr_image[$val['id']] = $this->websiteUrl.$val['image'];
        }
        $arr_config = [];
        $config_ids = explode(",",$info['config_ids']);
        foreach($config_ids as $k=>$v){
            $arr_config[$k]['name'] = $arr_name[$v];
            $arr_config[$k]['image'] = $arr_image[$v];
        }
        //看房记录
        $list_visit = db('yjy_house_visit_log')
            ->field("id,renter_name as name,from_unixtime(createtime,'%Y/%m/%d %H:%i') as time")
            //->where('agent_id',$userInfo['id'])
            ->where("house_id",$house_id)
            ->select();
        $data = [
            'swiperList' => $topImage,
            'house_info'  => $info,
            'tags' => $arr_tags,
            'configList' => $arr_config,
            'visitList'  => $list_visit,
            'login_person_id' => $userInfo['id']
        ];
        $this->success("返回成功",$data);

    }


    //房源编辑
    public function houseDetailEdit(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        ob_clean();
        $house_id = $this->request->request("house_id");
        //$house_id = 80;
        $info = db("yjy_house a")
            ->join("yjy_house_layout b","a.layout_id = b.id","LEFT")
            ->join("yjy_house_area c","a.area_id = c.id","LEFT")
            ->join("yjy_house_orientations d","a.orientations_id = d.id","LEFT")
            ->join("yjy_house_landlord e","e.id = a.landlord_id")
            ->field("a.*,b.name as layoutName,c.name as areaName,d.name as orientationsName")
            ->field("a.location_map as locationAddress,e.name as agentname")
            ->where("a.Id",$house_id)
            ->find();

        $list_layout = db("yjy_house_layout")->order("id asc")->column("name");
        $items_layout = array_search($info['layoutName'],$list_layout);
        $info['curLayout'] = $items_layout + 1;

        $list_area = db("yjy_house_area")->order("id asc")->column("name");
        $items_area = array_search($info['areaName'],$list_area);
        $info['curArea'] = $items_area + 1;

        $list_orientations = db("yjy_house_orientations")->order("id asc")->column("name");
        $items_orientations = array_search($info['orientationsName'],$list_orientations);
        $info['curOrientations'] = $items_orientations + 1;

        //房源配置
        $arr_configs = explode(",",$info['config_ids']);
        $arr_configName = [];
        foreach($arr_configs as $key=>$val){
            $configName = db("yjy_house_config")->where("id",$val)->value("name");
            array_push($arr_configName,$configName);
        }
        $info['arr_configName'] = $arr_configName;

        //房源标签
        $arr_tags = explode(",",$info['tags_ids']);
        $arr_tagsName = [];
        foreach($arr_tags as $key=>$val){
            $tagsName = db("yjy_house_tags")->where("id",$val)->value("name");
            array_push($arr_tagsName,$tagsName);
        }
        $info['arr_tagsName'] = $arr_tagsName;
        //顶部图片
        $info['images'] = explode(",",$info['images']);
        foreach($info['images'] as $key=>$val){
            $info['images'][$key] = $this->websiteUrl.$val;
        }
        //顶部视频
        if($info['video']){
            $info['video'] = $this->websiteUrl.$info['video'];
        }

        $this->success("返回成功",$info);
    }


    //房源编辑操作
    public function houseDetailEditOperate(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        ob_clean();
        $house_id = $this->request->request("house_id");
        $title = $this->request->request("title");
        $houseArea = $this->request->request("house_area");
        $houseOrientations = $this->request->request("house_orientations");
        $houseFloor = $this->request->request("house_floor");
        $content = $this->request->request("content");
        $houseConfig = $this->request->request("house_config");
        $houseLayout = $this->request->request("house_layout");
        $area_id = $this->request->request("area_id");
        $houseTags = $this->request->request("house_tags");
        $price = $this->request->request("price");
        $location_near = $this->request->request("location_near");
        $location_address = $this->request->request("location_address");
        $lon = $this->request->request("lon");
        $lat = $this->request->request("lat");
        $is_recommend = $this->request->request("is_recommend");
        $landlord_id = $this->request->request("landlord_id");
        $images = $this->request->request("images");
      //图片处理
        $arr_images = explode(",",$images);

        foreach($arr_images as $key=>$val){
           $domain =  strstr($val,$this->websiteUrl);
            if($domain){
                $arr_images[$key] = array_filter(explode($this->websiteUrl,$val))[1];
            }
        }
       $images = implode(',',array_values($arr_images));
        $video = $this->request->request("video");
        //视频处理
        $domain2 =  strstr($video,$this->websiteUrl);
        if($domain2){
            $video = array_filter(explode($this->websiteUrl,$video))[1];

        }
        if($is_recommend == true){$is_recommend = 1;}
        if($is_recommend == false){$is_recommend = 0;}
        $arr_config = explode(",", $houseConfig);
        $config_ids = [];
        foreach($arr_config as $val){
            $config = db("yjy_house_config")->field("id,name")->select();
            foreach($config as $k=>$v){
                if($val == $v['name']){array_push($config_ids,$v['id']);}
            }
        }
        $config_ids_2 = implode(',',$config_ids);
        $arr_tags = explode(",", $houseTags);
        $tags_ids = [];
        foreach($arr_tags as $val){
            $tags = db("yjy_house_tags")->field("id,name")->select();
            foreach($tags as $k=>$v){
                if($val == $v['name']){array_push($tags_ids,$v['id']);}
            }
        }
        $tags_ids_2 = implode(',',$tags_ids);
        $params_house=[
            'title'       =>$title,
            'house_area'  =>$houseArea,
            'orientations_id' =>$houseOrientations,
            'house_floor' =>$houseFloor,
            'content'       =>$content,
            'config_ids'=>$config_ids_2,
            'layout_id'=>$houseLayout,
            'area_id'  =>$area_id,
            'tags_ids'  =>$tags_ids_2,
            'price'       =>$price,
            'location_near'=>$location_near,
            'location_map'=>$location_address,
            'lon'        =>$lon,
            'lat'        =>$lat,
            'is_recommend'=>$is_recommend,
            'createtime'  =>time(),

            'landlord_id' =>$landlord_id,
            'images'      =>$images,
            'video'       =>$video
        ];
        $res = db("yjy_house")->where("Id",$house_id)->update($params_house);
        if($res){
            $this->success("房源编辑成功",$house_id);
        }else{
            $this->error("房源编辑失败");
        }

    }




    //添加合同信息
    public function addAgreement(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        ob_clean();
        $userInfo = $this->auth->getUserinfo();
        $house_id = $this->request->request("house_id");
        $deal_name = $this->request->request("deal_name");

        $in_time = $this->request->request("in_time");
        $agreement_images = $this->request->request("agreement_images");
        db("yjy_house")->where("id",$house_id)->update(['status'=>1]);
        $res = db("yjy_deal")->insertGetId([
            'agent_id' => $userInfo['id'],
            'house_id' => $house_id,
            'renter_name' => $deal_name,
            'intime'  => strtotime($in_time),
            'agreement_images' => $agreement_images,
            'createtime' => time()
        ]);
        if($res){
            $this->success("添加成功");
        }else{
            $this->error("添加失败");
        }
    }



    //房源轮播视频
    public function video(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        ob_clean();
        $house_id = $this->request->request("house_id");
        $video = db("yjy_house")->where("Id",$house_id)->value("video");
        $this->success("返回成功",$video);
    }




}